2869번 달팽이는 올라가고 싶다
Day8 8단계 20231026
풀이 1
- 내가 처음에 짠 코드는 Arithmetic 오류가 계속 떠서 사용하지 못하겠다
import java.io.*;
import java.math.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
double climb = Double.parseDouble(st.nextToken());
double decline = Double.parseDouble(st.nextToken());
double tree = Double.parseDouble(st.nextToken());
if (climb == tree) {
System.out.println(1);
} else {
BigDecimal bc = BigDecimal.valueOf((tree - decline)).divide(BigDecimal.valueOf((climb - decline)));
double day = bc.setScale(0, RoundingMode.CEILING).doubleValue();
System.out.printf("%.0f", day);
}
br.close();
}
}
- 나머지가 0이 아니면 그걸 보정해주기 위해 소수점 첫째자리를 올림해준건데 이게 뭔가 문제가 있던듯 함.
- 나머지가 0이 아니면 몫에 +1을 해주고, 나머지가 0이면 몫을 반환하도록 변경
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int climb = Integer.parseInt(st.nextToken());
int decline = Integer.parseInt(st.nextToken());
int tree = Integer.parseInt(st.nextToken());
if (climb == tree) {
System.out.println(1);
} else {
int day = ((tree -decline)%(climb - decline)==0) ? (tree -decline)/(climb - decline) : (tree -decline)/(climb - decline)+1;
System.out.print(day);
}
br.close();
}
}
풀이 2
- 풀이 날짜 : 2025.05.23
- 약 1년 만에 부트 캠프에서 문제를 풀면서 다시 풀게 되었다.
- 처음엔
while문을 사용해서 올라간 값과 내려간 값을 계속 증감시켰다가 시간 초과 조건이 있다는 것을 깜빡하여 다시 풀었다. - 문제에서 달팽이는 낮에 올라가고 밤에 내려오며, 정상에 도달한 후엔 미끄러지지 않으므로 정상에 도달했던 날을 계산하면 된다.
- 이 때 연산은
double로 하고,Math.ceil()을 사용하여 계산 결과를 반올림하여 수식 첫 줄의 "Vm 이상" 조건을 맞추었다. - 그리고 계산 결과를 다시
int로 변환하여 day를 출력했다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
BufferedReader reader;
try {
reader = new BufferedReader(new InputStreamReader(System.in));
String[] condition = reader.readLine().split(" ");
int up = Integer.parseInt(condition[0]);
int down = Integer.parseInt(condition[1]);
int height = Integer.parseInt(condition[2]);
int day = (int) Math.ceil((double)(height - down)/(up - down));
System.out.println(day);
} catch (Exception e) {
}
}
}